package com.tebiecloud.uaa.admin.server.service.impl;

import com.tebiecloud.uaa.admin.server.service.feign.BaseAppServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: tebie
 * @Date: 2019-08-10 09:27
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ClientDetailsServiceImpl implements ClientDetailsService {

    @Autowired
    private BaseAppServiceClient baseAppServiceClient;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        ClientDetails details = baseAppServiceClient.getClientDetails(clientId).getData();
        if (details != null && details.getAdditionalInformation() != null) {
            String status = details.getAdditionalInformation().getOrDefault("status", "0").toString();
            if(!"1".equals(status)) {
                throw new ClientRegistrationException("客户端已被禁用");
            }
        }
        return details;
    }

//    /**
//     * 生成测试的认证客户端
//     * @return
//     */
//    public static SecurityClient createTestClient() {
//        SecurityClient details = new SecurityClient();
//        details.setClientId("1552274783265");
//        details.setClientSecret("$2a$10$xxfI6N5kSKINJXipQb9dJuS1Z7T2z4h/IZrhNnx3zNzCViCQMDtfq");
//        List<String> listResourceIds = Lists.newArrayList();
//        details.setResourceIds(listResourceIds);
//        List<String> listScope = Lists.newArrayList();
//        listScope.add("userprofile");
//        details.setScope(listScope);
//        List<String> listAuthorizedGrantTypes = Lists.newArrayList();
//        listAuthorizedGrantTypes.add("authorization_code");
//        listAuthorizedGrantTypes.add("client_credentials");
//        listAuthorizedGrantTypes.add("implicit");
//        listAuthorizedGrantTypes.add("password");
//        listAuthorizedGrantTypes.add("refresh_token");
//        details.setAuthorizedGrantTypes(listAuthorizedGrantTypes);
//        Set<String> listRegisteredRedirectUri = Sets.newHashSet();
//        listRegisteredRedirectUri.add("http://www.baidu.com");
//        listRegisteredRedirectUri.add("http://localhost:8003/login");
//        listRegisteredRedirectUri.add("http://localhost:8003/webjars/springfox-swagger-ui/o2c.html");
//        details.setRegisteredRedirectUri(listRegisteredRedirectUri);
//        details.setAccessTokenValiditySeconds(43200);
//        details.setRefreshTokenValiditySeconds(2592000);
//        Map<String, Object> mapAdditionalInformation = Maps.newHashMap();
//        mapAdditionalInformation.put("appIcon", "");
//        mapAdditionalInformation.put("website", "http://www.baidu.com");
//        mapAdditionalInformation.put("appName", "资源服务器");
//        mapAdditionalInformation.put("appType", "");
//        mapAdditionalInformation.put("server", "");
//        mapAdditionalInformation.put("appDesc", "资源服务器");
//        mapAdditionalInformation.put("appId", "1552274783265");
//        mapAdditionalInformation.put("appNameEn", "ResourceServer");
//        mapAdditionalInformation.put("updateTime", 1553011137731L);
//        mapAdditionalInformation.put("userType", "platform");
//        mapAdditionalInformation.put("userId", 521677655146233856L);
//        mapAdditionalInformation.put("appOs", "");
//        mapAdditionalInformation.put("status", 1);
//        details.setAdditionalInformation(mapAdditionalInformation);
//        return details;
//    }

}
